// pages/order/allOrder/allOrder.js
//const app = getApp();
var api = require("../../../api.js"),
    app = getApp();
var handler=require("../../../utils/function.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    polyline: [{
      points: [],
      color: '#000000',
      width: 5
    }],
    markers: [],
    location: {
      lat: '',
      lng: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    app.request({
      url: api.order.track,
      method: 'post',
      data: {
        order_no: options.order_no
      },
      success: function (res) {
        var newPolyline  = JSON.parse(res.data.position_point);
        // var newPolyline  = JSON.parse(wx.getStorageSync('polyline'));
        // var distance = that.getDistance(newPolyline[0].latitude,newPolyline[0].longitude, newPolyline[newPolyline.length-1].latitude,newPolyline[newPolyline.length-1].longitude);
        // for (let i = 0; i < newPolyline.length-1; i++) {
        //   distance += that.getDistance(newPolyline[i].latitude,newPolyline[i].longitude, newPolyline[i + 1].latitude,newPolyline[i + 1].longitude)
        // }

        // console.log(distance)
        var makes = [{
          id: 0,
          iconPath: "../../../images/user/icon-44.png",
          latitude: newPolyline[0].latitude,
          longitude: newPolyline[0].longitude,
          width: 40,
          height: 40
        }, {
          id: 1,
          iconPath: "../../../images/user/icon-43.png",
          latitude: newPolyline[newPolyline.length-1].latitude,
          longitude: newPolyline[newPolyline.length-1].longitude,
          width: 40,
          height: 40
        }];
        var location = {
          lat: newPolyline[0].latitude,
          lng: newPolyline[0].longitude
        };
        that.setData({
          location,
          polyline: [{
            points: newPolyline,
            color: '#000000',
            width: 7
          }],
          makes
        })
      }
    })
  },

  getDistance: function(lat1, lng1, lat2, lng2){
    var radLat1 = lat1*Math.PI / 180.0;
    var radLat2 = lat2*Math.PI / 180.0;
    var a = radLat1 - radLat2;
    var b = lng1*Math.PI / 180.0 - lng2*Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
    Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
    s = s *6378.137 ;// EARTH_RADIUS;
    s = Math.round(s * 10000) / 10000;
    return s;
    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})